package icu.luoxi.common;

import icu.luoxi.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

/**
 * 全局异常处理类
 * @author Cao Jiwen
 * @version V1.0
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessExceptionHandler(BusinessException e) {
        log.error("业务异常,原因:", e);
        return ResultUtils.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    public BaseResponse nullPointerExceptionHandler(NullPointerException e) {
        log.error("空指针异常,原因:", e);
        return ResultUtils.error(ErrorCode.PARAMS_ERROR);
    }

    @ExceptionHandler(DataIntegrityViolationException.class)
    public BaseResponse dataIntegrityViolationExceptionHandler(DataIntegrityViolationException e){
        log.error("数据异常,原因:", e);
        return ResultUtils.error(ErrorCode.PARAMS_ERROR);
    }
    @ExceptionHandler(Exception.class)
    public BaseResponse exceptionHandler(Exception e){
        log.error("未知异常",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR);
    }
}
